//获取Token参数
	var Request = new Object();
	Request = GetRequest();
	var token= Request['Token'];
	$('.mb').hide();
	$('.datagrid-sort-icon').hide();	
  	
$(document).ready(function(){
	var grid=$('#grid').datagrid({
		width:'auto',
		height:'auto',
		fit:true,
		nowrap: false,
		striped: true,
		collapsible : false,
		singleSelect:true,
		url:'action/master/usergroup/list.json?Token=' + token,
			sortName: 'F_CODE',
			sortOrder: 'asc',
			remoteSort: false,
			loadMsg:'正在载入数据...',
			idField:'F_ID',
			fitColumns:true,
			columns:[[
//			          {checkbox:true},
			          {field:'F_CODE',title:"<div class='tCol' style='display: block;padding:0px;width:100%; text-align:center;' ><span style='float: right;width: 100%;'>编码</span></div>",width:50,sortable:true,align:'left'},
			          {field:'F_NAME',title:"<div class='tCol' style='display: block;padding:0px;width:100%; text-align:center;' ><span style='float: right;width: 100%;'>组名</span></div>",width:150,sortable:true,align:'left'},
			          {field:'F_MEMO',title:"<div class='tCol' style='display: block;padding:0px;width:100%; text-align:center;' ><span style='float: right;width: 100%;'>备注</span></div>",width:300,sortable:true,align:'left'}
			          ]],
		pagination:true,
		rownumbers:false,
		onBeforeLoad:function(param){
			$('.mb').hide();
			$('.datagrid-sort-icon').hide();	
			
			$('div.datagrid-view2>div.datagrid-body').css('overflow','hidden');
			$('div.datagrid-view2>div.datagrid-header').attr('id','gheader');
			$('#gheader').css('height','35px');
			$('#gheader>div.datagrid-header-inner').attr('id','gInner');
			$('#gInner>table').attr('id','gTable');
			$('#gTable').css('height','35px');
			$('#gTable').find('td').css('height','30px');
			$('#gTable').find('td').css('line-height','30px');
									},
		onLoadSuccess:function(){
			// 默认选中第一行
			if($(this).datagrid("getRows").length > 0){
//				$(this).datagrid("selectRow", 0);
			}
			$('div.datagrid-view2>div.datagrid-body').css('overflow','hidden');
			
			$('div.datagrid-view2>div.datagrid-body').css('overflow','hidden');
			$('div.datagrid-view2>div.datagrid-header').attr('id','gheader');
			$('#gheader').css('height','35px');
			$('#gheader>div.datagrid-header-inner').attr('id','gInner');
			$('#gInner>table').attr('id','gTable');
			$('#gTable').css('height','35px');
			$('#gTable').find('td').css('height','30px');
			$('#gTable').find('td').css('line-height','30px');
			
		},onDblClickRow:function(rowIndex,rowData){
			update();
		},toolbar:[{
			id:'btnRefresh',
			text:'刷新',
			iconCls:'icon-reload',
			handler:function(){
			  GirdReload();
			}
		 },'-',{
			id:'btnadd',
			text:'新增',
			iconCls:'icon-add',
			handler:function(){
				$("#addForm").attr("action","action/master/usergroup/add.json?Token=" + token);
				$.getJSON("action/master/usergroup/getMaxCode.json?Token=" + token+"&rnd="+Math.random(), function(data){
                        $("#addForm input[name=F_CODE]").val(data.F_CODE);
                });
				openDialog('新增','addDig','grid','addForm',{'height': 240,'width':340,clearForm:true},
					function(){ 
						$("#addDig").dialog('open');
						$.getJSON("action/master/usergroup/getMaxCode.json?Token=" + token+"&rnd="+Math.random(), function(data){
                        $("#addForm input[name=F_CODE]").val(data.F_CODE);
                });
					}
				);
			}
		},'-',{
			id:'btnEdit',
			text:'修改',
			iconCls:'icon-edit',
			handler:function(){
				update();
			}
		},'-',{
			id:'btnDel',
			text:'删除',
			iconCls:'icon-no',
			handler:function(){
				$("#deleteForm").attr("action","action/master/usergroup/del.json?Token=" + token);
				var select = $('#grid').datagrid('getSelected');
				if(select)
				{
					var msg = "您确定要删除用户组【" + select.F_CODE + " " + select.F_NAME + "】吗?";
					$("#deleteForm input[name=F_ID]").val(select.F_ID);
					del("grid","delId","deleteForm","F_ID",msg,function(){
					$('#grid').datagrid('clearSelections');
				});
				}	
				else
				{
					 parent.$.messager.show({title:'消息',msg:"<p style='font-family:'宋体',Arial;font-size:14px;'>请选择一条数据!</p>",timeout:3000,showType:'slide',width:250,height:150});
				}
			}
		}
		]
	});
	$('#addForm input').focusout(trimkeyup);
	$('#updateForm input').focusout(trimkeyup);
	$('#queryForm input').focusout(trimkeyup);
	$('#txtcode').keydown(onlyNum);
//	$('#txtcode').onbeforepaste(onlyNum);
	$('#addcode').keydown(onlyNum);
//	$('#addcode').onbeforepaste(onlyNum);
	
	$('div.datagrid-view2>div.datagrid-body').css('overflow','hidden');
	$('div.datagrid-view2>div.datagrid-header').attr('id','gheader');
	$('#gheader').css('height','35px');
	$('#gheader>div.datagrid-header-inner').attr('id','gInner');
	$('#gInner>table').attr('id','gTable');
	$('#gTable').css('height','35px');
	$('#gTable').find('td').css('height','30px');
	$('#gTable').find('td').css('line-height','30px');
	
});
//clearSelect("UserGrid")
//formatpagination("UserGrid")
function trimkeyup(e) {
    lucene_objInput = $(this);
    if (e.keyCode != 38 && e.keyCode != 40 && e.keyCode != 13) {
        var im = $.trim(lucene_objInput.val());
        lucene_objInput.val(im); 
    }
} 

//编码禁止输入数字以外字符
function onlyNum(e) 
{ 
	lucene_objInput = $(this);
	var im = $.trim(lucene_objInput.val());
	
	//第一个if定义除数字外可响应的键,如46对应Delete键,若要响应Tab键,可在后面加上&&!(event.keyCode==9),若要允许输入小数点,则可加上&&!(event.keyCode==190)&&!(event.keyCode==110),其它类似 
	if(!(e.keyCode==46)&&!(e.keyCode==8)&&!(e.keyCode==37)&&!(e.keyCode==39)) 
	{
	//第二个if定义要响应的数字键,||前面的是响应左边键盘对应的数字,后面是响应小键盘上的数字 
	    if(!((e.keyCode>=48&&e.keyCode<=57)||(e.keyCode>=96&&e.keyCode<=105))) 
	    {
	    	return false;
	    }
	}		    	 
} 

function GirdReload()
{
	$('#grid').datagrid("options").pageNumber=1;
				$('#grid').datagrid({
					url : 'action/master/usergroup/list.json?Token='+ token,
					'queryParams':{
//    				'F_CODE':"",
//    				'F_NAME':"",
    				'F_ID':""
				}
				});
}
function showScroll(){
	$('div.datagrid-view2>div.datagrid-body').css('overflow','auto');
}

function hideScroll(e,o){
	if(o.contains(event.toElement ) == false) 
		$('div.datagrid-view2>div.datagrid-body').css('overflow','hidden');
	
}
function update(){
	$("#updateForm").attr("action","action/master/usergroup/edit.json?Token=" + token);
	var select = $('#grid').datagrid('getSelected');
    if(select){
    	$("#updateForm input[name=F_ID]").val(select.F_ID);
    	$("#updateForm input[name=F_CODE]").val(select.F_CODE);
    	$("#updateForm input[name=F_NAME]").val(select.F_NAME);
    	$("#updateForm input[name=F_MEMO]").val(select.F_MEMO);
    	openDialog('修改','updateDig','grid','updateForm',{'height':240,'width':340,'RowID':select.F_ID,clearForm:false},
    		function(){
//    			$('#grid').datagrid("options").pageNumber=1;
//				$('#grid').datagrid({
//					url : 'action/master/usergroup/list.json?Token='+ token,
//					'queryParams':{
//    				'F_ID':select.F_ID
//				}
//				});
    		});
   	}else{    	
   	parent.$.messager.show({title:'消息',msg:"<p style='font-family:'宋体',Arial;font-size:14px;'>请选择一行数据!</p>",timeout:2000,showType:'slide',width:250,height:150});
   	}
}

//$.extend($.fn.validatebox.defaults.rules, { 
//    checkEmptyCode:{
//    	validator: function(value, param){
//		    return /^\d+$/.test(value);
//		},
//		message: '只能输入数字'
//    },
//    checkEmptyName:{
//    	validator: function(value, param){
//		    var text =  $.trim(value);
//		    return (text.length>0)
//		},
//		message: '名称不能为空'
//    }
//});